package team.knowers.firm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.firm.common.database.BaseDO;
import team.knowers.firm.dao.entity.BookingDO;
import team.knowers.firm.dao.entity.FlightDO;
import team.knowers.firm.dao.entity.NoticeDO;
import team.knowers.firm.dao.mapper.BookingMapper;
import team.knowers.firm.dao.mapper.FlightMapper;
import team.knowers.firm.dao.mapper.NoticeMapper;
import team.knowers.firm.dao.mapper.PaymentMapper;
import team.knowers.firm.dto.req.AddNoticeReqDTO;
import team.knowers.firm.dto.req.PageNoticeInfoReqDTO;
import team.knowers.firm.service.NoticeService;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticeDO> implements NoticeService {
    private final NoticeMapper noticeMapper;
    private final BookingMapper bookingMapper;
    private final PaymentMapper paymentMapper;
    private final FlightMapper flightMapper;

    @Override
    public Boolean addNotice(AddNoticeReqDTO req) {
        NoticeDO noticeDO = NoticeDO.builder()
                .title(req.getTitle())
                .content(req.getContent())
                .flightId(req.getFlightId())
                .flightNo(req.getFlightNo())
                .build();
        return noticeMapper.insert(noticeDO) > 0;
    }

    @Override
    public Boolean deleteNotice(int id) {
        return noticeMapper.deleteById(id) > 0;
    }

    @Override
    public List<NoticeDO> getNoticesPerPeople(int userId) {
        LambdaQueryWrapper<BookingDO> eq = Wrappers.lambdaQuery(BookingDO.class).eq(BaseDO::getDelFlag, 0).eq(BookingDO::getTouristId, userId).orderByDesc(BaseDO::getCreateTime);
        List<BookingDO> bookingDOS = bookingMapper.selectList(eq);
        //获得所有航班号
        List<Integer> flightIds = bookingDOS.stream().map(BookingDO::getFlightId).collect(Collectors.toList());
        return noticeMapper.selectList(Wrappers.lambdaQuery(NoticeDO.class)
                .eq(BaseDO::getDelFlag, 0)
                .and(wrapper -> wrapper.isNull(NoticeDO::getUserId)
                        .or().eq(NoticeDO::getUserId, userId))
                .in(NoticeDO::getFlightId, flightIds)
                .orderByDesc(NoticeDO::getCreateTime));
    }

    @Override
    public IPage<NoticeDO> getAllNoticesPage(PageNoticeInfoReqDTO reqDTO) {
        return noticeMapper.selectPage(reqDTO, Wrappers.lambdaQuery(NoticeDO.class).eq(NoticeDO::getDelFlag, 0).orderByDesc(NoticeDO::getCreateTime));
    }

    @Override
    public Boolean addNoticeCancelFlight(int flightId) {
        FlightDO flightDO = flightMapper.selectById(flightId);
        String title = "您的"+flightDO.getFlightNo()+"次航班被取消";
        String content = "十分抱歉，您的"+flightDO.getFlightNo()+"次航班被取消,我们已为您进行退款,请您及时重新安排行程";
        NoticeDO build = NoticeDO.builder().flightNo(flightDO.getFlightNo()).title(title).content(content)
                .flightId(flightId).userId(null).build();
        return noticeMapper.insert(build) > 0;
    }

    @Override
    public Boolean addNoticeUpdateFlight(FlightDO req) {
        String title = "您的"+req.getFlightNo()+"次航班信息有所更改";
        String content = "您好，您的"+req.getFlightNo()+"次航班信息有所更改,请您及时查看";
        NoticeDO build = NoticeDO.builder()
                .title(title)
                .content(content)
                .userId(null)
                .flightId(req.getId())
                .flightNo(req.getFlightNo())
                .build();
        return noticeMapper.insert(build) > 0;
    }
}
